﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Aliyun.OSS.Sdk.JSST
{
    public class PutObjectFile
    {
        /// <summary>
        /// OSS图片发送类
        /// </summary>
        private PutObject putObject;

        /// <summary>
        /// 信号量 同步转异步调用
        /// </summary>
        private AutoResetEvent autoResetEvent = new AutoResetEvent(false);

        /// <summary>
        /// OSS参数1
        /// </summary>
        private string bucketName;

        /// <summary>
        /// OSS参数2
        /// </summary>
        private string key;

        /// <summary>
        /// OSS参数3
        /// </summary>
        private string localFilePath;

        /// <summary>
        /// 0:成功 1：失败 100：配置信息错误 101：其他异常
        /// </summary>
        private int resultInt;
        /// <summary>
        /// 返回消息
        /// </summary>
        private Result resultMessage;

        /// <summary>
        /// 发送图片线程
        /// </summary>
        private Thread threadPutObjectFromFile;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="putObject"></param>
        /// <param name="bucketName"></param>
        /// <param name="key"></param>
        /// <param name="localFilePath"></param>
        public PutObjectFile(PutObject putObject, string bucketName, string key, string localFilePath)
        {
            this.putObject = putObject;
            this.bucketName = bucketName;
            this.key = key;
            this.localFilePath = localFilePath;
        }

        /// <summary>
        /// 发送图片
        /// </summary>
        /// <param name="resultMessage"></param>
        /// <returns></returns>
        public int PutObjectFromFile(out Result resultMessage)
        {
            DateTime createTime = DateTime.Now;
            resultMessage = new Result();

            threadPutObjectFromFile = new Thread(new ThreadStart(PutObjectFromFile));
            threadPutObjectFromFile.Start();

            //最多等待20秒
            bool bol= autoResetEvent.WaitOne(20000);
            if (bol == false)//属于信号量超时后返回
            {
                //超时后先杀掉发送线程
                try
                {
                    threadPutObjectFromFile.Abort();
                }
                catch (Exception)
                {

                }
                //杀掉后需要重新初始化
                putObject.Ini();
                resultInt = 101;
                resultMessage = new Result();
                resultMessage.ErrorCode = "";
                resultMessage.Status = 1;
                resultMessage.Message = string.Format("创建时间：{0}，当前时间：{1}，已超时超时直接返回！"
                    , createTime.ToString("yyyy-MM-dd HH:mm:ss")
                    , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            resultMessage = this.resultMessage;
            return resultInt;
        }

        private void PutObjectFromFile()
        {
            resultInt = putObject.PutObjectFromFile(bucketName, key, localFilePath, out resultMessage);
            autoResetEvent.Set();
        }
    }
}
